//
//  LXPermissionCamera.swift
//  LXPermissionAuth
//
//  Created by yoctech on 2021/8/18.
//

import UIKit
import AVFoundation

@objc class LXPermissionCamera: NSObject, LXPermissionProtocol {
    @objc public static func auth(autoRequestIfNot: Bool, clouser: @escaping LXAuthClouser) {
        let aStatus = AVCaptureDevice.authorizationStatus(for: .video)
        switch aStatus {
        case .notDetermined:
            if !autoRequestIfNot {
                clouser(false,  .other)
                return
            }
            AVCaptureDevice.requestAccess(for: .video) { (result) in
                if result{
                    DispatchQueue.main.async {
                        clouser(true,  .authorized)
                    }
                }else{
                    DispatchQueue.main.async {
                        clouser(false,  .other)
                    }
                }
            }
        case .denied:
            clouser(false,  .denied)
        case .restricted:
            clouser(false,  .restricted)
        case .authorized:
            clouser(true,  .authorized)
        @unknown default:
            clouser(false,  .other)
        }
    }
    

}
